package com.oyb.servlet;

import com.oyb.entity.Admin;
import com.oyb.service.AdminServies;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.util.List;

@WebServlet(urlPatterns = "/newscaster")
public class RegisteringServlet extends HttpServlet {
    public static String uploaderPath = "D:\\uploader\\";

    private AdminServies adminServies = new AdminServies();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
        ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
        //文件上传路径编码格式
        servletFileUpload.setHeaderEncoding("utf-8");

        //解析请求
        //解析请求编码，因为有很多表单域和文本上传，并且每个表单域和文本上传都有值和名字...
        //所以把他们堪称对象，存在集合中，这个集合类型为fileitem
        List<FileItem> items =null;
        try {
           items = servletFileUpload.parseRequest(req);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

        //创建admin对象
        Admin admin = new Admin();
        //获得类对象
        Class cl1 = admin.getClass();
        System.out.println("456");
        if (items!=null){
            for (FileItem fileItem : items) {
                if(fileItem.isFormField()){
                 //普通表单域
                    System.out.println("表单项的名称：" + fileItem.getFieldName() + " 值：" + fileItem.getString("utf-8"));

                    //特殊处理权限类型 生日
                    if("birthday".equals(fileItem.getFieldName())){
                        admin.setBirthday(fileItem.getString("utf-8"));
                    } else if ("power".equals(fileItem.getFieldName())) {
                     //特殊处理权限类型权限
                        admin.addPower(fileItem.getString("utf-8"));
                    }else{
                    //其他普通类型的属性通过类对象获取私有属性
                        try {
                            Field field = cl1.getDeclaredField(fileItem.getFieldName());
                            field.setAccessible(true);
                            field.set(admin,fileItem.getString("utf-8"));
                        } catch (Exception e) {

                        }
                    }
                }else{
                 //文本上传
                    String filename = fileItem.getName();//文本名getName，普通表单域名getFieldName
                    InputStream in = fileItem.getInputStream();//文件上传的输入流
                    //输出流1.需要指定路径因为他不会想输入流那样自动创建路径2.路径必须指到文件名，而不是文件夹
                    OutputStream out = new FileOutputStream(uploaderPath + filename);
                    //输入流写到输出流,通过com我们导入的包的工具类实现文件的拷贝
                    IOUtils.copy(in,out);
                    admin.setPhoto(filename);
                }
            }
        }
        //存到数据库中
        int result = adminServies.register(admin);
        if(result>0){
            resp.sendRedirect("login.jsp");
        }else{
            resp.sendRedirect("register.jsp?status=1");
        }

    }
}













